#include <cstdio>
#include "zzconsole.h"

ZZConsole::ZZConsole()
{
	m_handle = NULL;
	m_lineNum = 0;
	m_colNum = 0;
}

ZZConsole::~ZZConsole()
{

}

void ZZConsole::init(int line, int col)
{
	m_lineNum = line;
	m_colNum = col;
	char cmd[200] = { 0 };
	sprintf_s(cmd, 200, "mode con cols=%d lines=%d", col, line);
	system(cmd);
	if (m_handle == NULL) {
		m_handle = GetStdHandle(STD_OUTPUT_HANDLE);
	}
}


void ZZConsole::exit()
{
	if (m_handle != NULL) {
		CloseHandle(m_handle);
	}
}

void ZZConsole::setTitle(const char *title)
{
	SetConsoleTitleA(title);
}

void ZZConsole::clearRegin(int line1, int col1, int line2, int col2)
{
	for (int i = line1; i <= line2; i++)
	{
		setPos(i, col1);
		for (int j = col1; j < col2; j++)
		{
			printf(" ");
		}
	}
	setPos(line1, col1);
}

void ZZConsole::displayStr(int line, int col, char *str)
{
	setPos(line, col);
	printf("%s", str);
}

void ZZConsole::displayInt(int line, int col, int data)
{

}

void ZZConsole::displayFloat(int line, int col, float data)
{

}

int ZZConsole::getLineNum()
{
	return m_lineNum;
}

int ZZConsole::getColNum()
{
	return m_colNum;
}

void ZZConsole::setPos(int line, int col)
{
	COORD pos;
	pos.X = col;
	pos.Y = line;
	SetConsoleCursorPosition(m_handle, pos);
}